package com.netease.xone.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.netease.xone.R;

public class LetterIndexView extends View implements OnTouchListener {
	
	public static final String[] CHARACTER_ARRAY = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
		"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
	private static final int PADDING = 0;
	
	boolean bDraw = true;
	
	// 平均高度
	int mPerHeight;
	// 字体大小
	int mWordSize;
	// 字体颜色
	int mColor;
	
	int mCurrentIndex = -1;
	
	boolean mIsTouch;
	
	OnLetterChangeListener mOnLetterChangeListener;
	
	public LetterIndexView(Context context) {
	    super(context);
	    init();
	}

	public LetterIndexView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
	    setOnTouchListener(this);
	    mWordSize = getResources().getDimensionPixelSize(R.dimen.discuss_text_18);
	    mColor = getResources().getColor(R.color.content_gray);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		if (oldh > h) {
			bDraw = false;
		} else {
			bDraw = true;
		}
		super.onSizeChanged(w, h, oldw, oldh);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		
        int width = 0;
        int height = 0;
        
        switch (heightMode) {
        case MeasureSpec.EXACTLY:
        	height = heightSize;
        	break;
        	
        case MeasureSpec.AT_MOST:
        	height = heightSize;
        	break;
        	
        case MeasureSpec.UNSPECIFIED:
        	height = heightSize;
        	break;
        }
        
        mPerHeight = height / CHARACTER_ARRAY.length;
        
        switch (widthMode) {
        case MeasureSpec.EXACTLY:
        	width = widthSize;
        	break;
        
        case MeasureSpec.AT_MOST:
        	width = Math.min(mPerHeight + 2 * PADDING + getPaddingLeft() + getPaddingRight(), widthSize);
        	break;
        	
        case MeasureSpec.UNSPECIFIED: 
        	width = mPerHeight + 2 * PADDING + getPaddingLeft() + getPaddingRight();
        	break;
        }
        
        setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (!bDraw) {
			return;
		}
		
		int height = getHeight();
		
		mPerHeight = height / CHARACTER_ARRAY.length;
		
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		paint.setARGB(255 * 20 / 100, 0, 0, 0);
		paint.setStyle(Style.FILL);
		
		if (mIsTouch) {
			canvas.drawRoundRect(new RectF(0 + getPaddingLeft(), 0, getWidth() - getPaddingRight(), getHeight()), 10, 10, paint);
		}
		
		paint.setColor(mColor);
		paint.setTextSize(mWordSize);
		paint.setTextAlign(Paint.Align.CENTER);
		int x = (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
		int y = mPerHeight;
		for (int i = 0; i < CHARACTER_ARRAY.length; i++) {
			if (mIsTouch && i == mCurrentIndex) {
			    paint.setColor(mColor);
			}	
			else {
			    paint.setColor(mColor);
			}
			canvas.drawText(CHARACTER_ARRAY[i], x, y, paint);
			y += mPerHeight;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
			mIsTouch = true;
			float y = event.getY();
			if (mPerHeight != 0) {
				mCurrentIndex = (int) (y / mPerHeight);
				if (mOnLetterChangeListener != null 
				        && mCurrentIndex >= 0
				        && mCurrentIndex < CHARACTER_ARRAY.length) {
					mOnLetterChangeListener.onLetterChange(mCurrentIndex);
				}
			}
			postInvalidate();
			
			return true;
		}
		else if (event.getAction() == MotionEvent.ACTION_UP) {
			mIsTouch = false;
			postInvalidate();
			
			return true;
		}
		
		return false;
	}
	
	public void setOnLetterChangeListener(OnLetterChangeListener onLetterChangeListener) {
		mOnLetterChangeListener = onLetterChangeListener;
	}
	
	public interface OnLetterChangeListener {
		public void onLetterChange(int index);
	}

}
